/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display;

import deatax.display.particle.ParticleSpawn;
import java.util.ArrayList;
import java.util.List;
import javax.media.opengl.GL2;

/**
 *
 * @author Caroline
 */
public class Model {

    public static int word = 8;

    public int glDrawList = -1;
    
    List<ModelPart> parts = new ArrayList<ModelPart>();

//    Shape3D shape;
    Texture texture;
    

//    public Model(Shape3D shape) {
//        this.shape = shape;
//    }

    static List<Model> cache = new ArrayList<Model>();
    
    public static void clearIds(){
        for (Model model : cache) {
            model.glDrawList = -1;
        }
    }
    
    public Model(){
        cache.add(this);
    }

    public void addPart(int size, float[] data, Texture t){
        parts.add(new ModelPart(size, data, t));
    }

    public void normalise(){
        float lowest = 999;
        for (ModelPart modelPart : parts) {
            lowest = Math.min(lowest, getLowestPoint(modelPart));
        }
        if(lowest == 0) return;
        for (ModelPart modelPart : parts) {
            shiftModel(modelPart,lowest);
        }

    }

    protected void shiftModel(ModelPart mp, float amount){
//        System.out.println("Shifting");
//        System.out.println(amount);
        for (int i = 6; i < mp.data.length; i+= word) {
            mp.data[i] -= amount;//data[word] - 100+ amount*10;
        }
    }

    public float getLowestPoint(ModelPart mp){
        float lowest = mp.data[6];
        for (int i = word+6; i < mp.data.length; i+= word) {
            if(mp.data[word] < lowest) lowest = mp.data[word];
        }
        return lowest;
    }

    public void draw(GL2 gl) {
//        int n = shape.numGeometries();
//        for (int i = 0; i < n; i++) {
//            drawGeometry(shape.getGeometry(i), gl);
//        }
        
        if(glDrawList > -1 && gl.glIsList(glDrawList)){
            gl.glCallList(glDrawList);
        } else {
            glDrawList = gl.glGenLists(1);
            gl.glNewList(glDrawList, GL2.GL_COMPILE_AND_EXECUTE);
        
            gl.glRotatef(90, 1, 0, 0);
            for (ModelPart modelPart : parts) {
                boolean textureBound = true;
                if(modelPart.texture != null){
                    gl.glEnable(GL2.GL_TEXTURE_2D); modelPart.texture.bind(gl);
                    
                } else if(texture != null){
                    gl.glEnable(GL2.GL_TEXTURE_2D); texture.bind(gl);
                } else {
                    textureBound = false;
                }
                gl.glBegin(GL2.GL_TRIANGLES);
                for (int i = 0; i < modelPart.data.length; i+=word) {
                    gl.glTexCoord2f(modelPart.data[i], 1-modelPart.data[i+1]);
                    gl.glNormal3fv(modelPart.data, i + 2);
                    gl.glVertex3fv(modelPart.data, i + 5);
                }
                gl.glEnd();
                if(textureBound){ gl.glDisable(GL2.GL_TEXTURE_2D); }
            }
            gl.glRotatef(-90, 1, 0, 0);
            gl.glEndList();
        }
        
        
    }

    public void setTexture(Texture texture) {
        this.texture = texture;
    }
    
    protected class ModelPart {
        float[] data;
        int size;
        Texture texture;

        public ModelPart(int size,float[] data, Texture texture) {
            this.data = data;
            this.size = size;
            this.texture = texture;
        }

    }
}
